<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务，帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件，不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types = 1);

namespace app\store\model\meal;

use app\common\model\meal\Menu as MenuModel;
use app\store\model\meal\MenuRel as MenuRelModel;
use think\facade\Db;

/**
 * 商品与分类关系模型
 * Class Menu
 * @package app\store\model
 */
class Menu extends MenuModel
{
    public function getListAll($date = '', $type = 0)
    {
        if ($date == '') {
            $date = Date('Y-m-d');
        } else {
            $dd = explode('T', $date);
            if (isset($dd[1])) {
                $cc   = explode(';', $dd[0]);
                $date = $cc[1];
                $date = strtotime($date) + 86400;
                $date = Date('Y-m-d', $date);
            }
        }

        $where = [];
        if ($type > 0) {
            $where['rel.type'] = $type;
        }
        $lists = Db::name('foods_menu_rel')->alias('rel')
            ->join('foods_menu m', 'm.menu_id=rel.menu_id')
            ->join('foods f', 'f.foods_id=rel.foods_id')
            ->where('m.date', $date)
            ->where($where)
            ->field('f.goods_name as name,rel.foods_id')
            ->select()->toArray();

        //     if(count($lists)>0){

        //     }
        $newdd = [];
        foreach ($lists as $key => $value) {
            $order['name'] = $value['name'];
            $order['num']  = Db::name('foods_order_goods')
                ->where('goods_id', $value['foods_id'])->where('date', $date)->count();
            $newdd[] = $order;
        }

        return $newdd;
    }

    public function add(array $data): bool
    {

        // 事务处理
        $this->transaction(function () use ($data) {
            $times    = between_time($data['betweenTime']);
            $date     = $times['start_time'];
            $end_time = $times['end_time'];

            for ($i = 0; $i < 30; $i++) {

                $menuData['store_id'] = self::$storeId;

                $menuData['date']        = Date('Y-m-d', $date);
                $menuData['create_time'] = time();
                $menu_id                 = Db::name('foods_menu')->insertGetId($menuData);

                isset($data['breakfastIds']) && MenuRelModel::increased((int) $menu_id, $data['breakfastIds'], 1);
                isset($data['lunchIds']) && MenuRelModel::increased((int) $menu_id, $data['lunchIds'], 2);
                isset($data['dinnerIds']) && MenuRelModel::increased((int) $menu_id, $data['dinnerIds'], 3);
                if ($end_time == $date) {
                    break;
                }
                $date += 86400;
            }

        });
        return true;
    }
    public function edit(array $data): bool
    {

        // 事务处理
        $this->transaction(function () use ($data) {
            $menu_id = $this['menu_id'];
            Db::name('foods_menu_rel')->where('menu_id', $this['menu_id'])->delete();

            isset($data['breakfastIds']) && MenuRelModel::increased((int) $menu_id, $data['breakfastIds'], 1);
            isset($data['lunchIds']) && MenuRelModel::increased((int) $menu_id, $data['lunchIds'], 2);
            isset($data['dinnerIds']) && MenuRelModel::increased((int) $menu_id, $data['dinnerIds'], 3);

        });
        return true;
    }
    public function getList($date)
    {
        $menu_id               = Db::name('foods_menu')->where('date', $date)->value('menu_id');
        $model                 = new MenuRelModel;
        $list['breakfastList'] = $model->getList($menu_id, 1);
        $list['lunchList']     = $model->getList($menu_id, 2);
        $list['dinnerList']    = $model->getList($menu_id, 3);

        return $list;

    }

}
